<?php
class ContactUsForm extends BaseForm {
  const FORM_NAME                 = 'frm';

  const FIELD_FIRST_NAME          = 'first_name';
  const FIELD_LAST_NAME           = 'last_name';
  const FIELD_EMAIL               = 'email';
  const FIELD_PHONE               = 'phone';
  const FIELD_SUBJECT             = 'subject';
  const FIELD_MESSAGE             = 'message';
  const FIELD_COPY_ME             = 'copy_me';

  public static $subjects         = array(
          'General Feedback/Comments',
          'Ticketing issue',
          'Account enquiry',
          'Cinema enquiry',
          'Careers at Supertain'
  );
  
  public function configure() {

    $widgets = array(
            self::FIELD_FIRST_NAME      => new sfWidgetFormInput(array(), array('maxlength' => 255)),
            self::FIELD_LAST_NAME       => new sfWidgetFormInput(array(), array('maxlength' => 255)),
            self::FIELD_EMAIL           => new sfWidgetFormInput(array(), array('maxlength' => 255)),
            self::FIELD_PHONE           => new sfWidgetFormInput(array(), array('maxlength' => 30)),
            self::FIELD_SUBJECT         => new sfWidgetFormSelect(array('choices' => self::$subjects), array('multiple' => false, 'expand' => true)),
            self::FIELD_MESSAGE         => new sfWidgetFormTextarea(array(), array()),
            self::FIELD_COPY_ME         => new sfWidgetFormInputCheckbox()
    );

    $labels = array(
            self::FIELD_FIRST_NAME      => 'First Name',
            self::FIELD_LAST_NAME       => 'Last Name',
            self::FIELD_EMAIL           => 'Email',
            self::FIELD_PHONE           => 'Phone',
            self::FIELD_SUBJECT         => 'Subject',
            self::FIELD_MESSAGE         => 'Message',
            self::FIELD_COPY_ME         => 'Send me a copy of my enquiry via email'
    );

    $this->setWidgets($widgets);
    $this->widgetSchema->setLabels($labels);

    $validators = array(
            self::FIELD_FIRST_NAME    => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required',array($labels[self::FIELD_FIRST_NAME])))),
            self::FIELD_LAST_NAME     => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_LAST_NAME])))),
            self::FIELD_EMAIL         => new sfValidatorEmail(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_EMAIL])), 'invalid' => LangUtil::getText('error_invalid', array($labels[self::FIELD_EMAIL])))),
            self::FIELD_SUBJECT       => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_SUBJECT])))),
            self::FIELD_MESSAGE       => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_MESSAGE]))))
    );

    $this->setValidators($validators);
    $this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'postValidator'))));

    $this->widgetSchema->setNameFormat(self::FORM_NAME . '[%s]');

    $this->validatorSchema->setOption('allow_extra_fields', true);
    $this->validatorSchema->setOption('filter_extra_fields', false);
  }

  public function postValidator($validator, $values) {
    $resp = recaptcha_check_answer(sfConfig::get('app_recaptcha_api_key_private'), ServerUtil::getUserIp(), $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);

    if (!$resp->is_valid) {
      throw new sfValidatorError($validator, LangUtil::getText('error_captcha'));
    }
    return $values;
  }
}
?>